<template>
  <section class="page">
    <headerbar>红友信息</headerbar>


    <section class="list-arrow mgt-2">
      <div class="item">
        <span class="title">姓名</span>
        <span class="title">{{userInfo.fullname}}</span>
      </div>
      <div class="item">
        <span class="title">性别</span>
        <span class="title">{{getSex(userInfo.sex)}}</span>
      </div>
      <div class="item">
        <span class="title">年龄</span>
        <span class="title">{{getAge(userInfo.birthday)}}</span>
      </div>
    </section>

  </section>
</template>

<script>
  import moment from 'moment'
  import tools from '@/utils/tools'

  export default {
    name: 'userInfo',
    data () {
      return {
        userInfo: {}
      }
    },
    created () {
      let id = this.$route.params.id
      this.loadData(id)
    },
    methods: {
      getTime (createTime) {
        if (!createTime) {
          return ''
        }
        return moment(createTime).format('YYYY年MM月DD日')
      },
      getSex (sex) {
        console.log('------------sex=', sex)
        if (!sex) {
          return ''
        }
        if (sex === 'Male') {
          return '男'
        } else {
          return '女'
        }
      },
      getAge (birthday) {
        if (!birthday || birthday === '') {
          return ''
        }
        birthday = this.getBirthDayTime(birthday)
        let birthdays = new Date(birthday.replace(/-/g, '/'))
        let d = new Date()
        let age =
          d.getFullYear() -
          birthdays.getFullYear() -
          (d.getMonth() < birthdays.getMonth() ||
          (d.getMonth() === birthdays.getMonth() &&
            d.getDate() < birthdays.getDate())
            ? 1
            : 0)
        return age
      },
      getBirthDayTime (createTime) {
        return moment(createTime).format('YYYY-MM-DD HH:mm')
      },
      loadData (id) {
        this.fetchService.getUserInfoById(id).then(res => {
          if (res.code === 0) {
            this.userInfo = res.data
          } else {
            return tools.tip(res.message)
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
